package com.atguigu.tingshu.user.api;

import cn.binarywang.wx.miniapp.api.WxMaService;
import com.atguigu.tingshu.common.auth.login.annotation.NeedLogin;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.result.ResultCodeEnum;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.user.service.UserInfoService;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Tag(name = "微信授权登录接口")
@RestController
@RequestMapping("/api/user/wxLogin")
@Slf4j
public class WxLoginApiController {

    @Autowired
    private UserInfoService userInfoService;


    //http://localhost:8500/api/user/wxLogin/wxLogin/0d3UVO000CS6RT1m5L000yOkpK2UVO01
    @GetMapping("/wxLogin/{code}")
    @Operation(summary = "微信登录")
    public Result wxLogin(@PathVariable(value = "code") String code) {
        Map<String, Object> map = userInfoService.wxLogin(code);
        return Result.ok(map);
    }

    //http://localhost:8500/api/user/wxLogin/getUserInfo

    @GetMapping("/getUserInfo")
    @Operation(summary = "获取登录用户信息")
    @NeedLogin
    public Result<UserInfoVo> getUserInfo() {
        Long userId = AuthContextHolder.getUserId();
        UserInfoVo userInfoVo = userInfoService.getUserInfo(userId);
        return Result.ok(userInfoVo);
    }

    @GetMapping("/refreshToken/getNewAccessToken")
    @Operation(summary = "生成一个新的accessToken")
    @NeedLogin
    public Result getNewAccessToken() {
        Map<String, Object> map = userInfoService.getNewAccessToken();
        return Result.ok(map);
    }

    //http://localhost:8500/api/user/wxLogin/updateUser
    @Operation(summary = "更新用户信息")
    @PostMapping("/updateUser")
    @NeedLogin
    public Result updateUser(@RequestBody UserInfo userInfo){
        userInfo.setId(AuthContextHolder.getUserId());
        userInfoService.updateUserById(userInfo);
        return Result.ok();
    }

}
